﻿using System.Data.Entity;
using NBlock.Core.ORM;
using NBlock.Core.ORM.EF;
using Ninject.Modules;
using Ninject.Web.Common;

namespace Cost.Models
{
    /// <summary>
    /// 服务注册类
    /// </summary>
    public class RegisterService : NinjectModule
    {
        public const string CONN_NAME = "Cost";
        public override void Load()
        {
            Bind<DbContextBase>().To<CostDbContext>().InRequestScope().Named(CONN_NAME).WithConstructorArgument("name", CONN_NAME);
            Bind(typeof(ICostRepository<>)).To(typeof(CostRepository<>)).InSingletonScope().WithConstructorArgument("connName", CONN_NAME);
        }
    }

    #region DbContext & IRepository
    public partial class CostDbContext : DbContextBase
    {
        static CostDbContext() { Database.SetInitializer<CostDbContext>(null); }
        public CostDbContext(string name) : base(name) { }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            ModelConfig.ModelCreating(modelBuilder);
            base.OnModelCreating(modelBuilder);
        }
    }

    /// <summary>
    /// OrgAuth库的数据访问接口
    /// </summary>
    /// <typeparam name="T">实体（一般对应于数据库表或视图）</typeparam>
    public interface ICostRepository<T> : IRepository<T> where T : class { }
    public class CostRepository<T> : EFRepository<T>, ICostRepository<T> where T : class
    {
        public CostRepository(string connName) : base(connName) { }
    }
    #endregion
}
